<template>
	<ele-modal :width="750" :visible="visible" :confirm-loading="loading" title="批量录入条码" :body-style="{ paddingBottom: '8px' }" @update:visible="updateVisible" @ok="save">
		<a-form :wrapper-col="{ md: { span: 24 }, sm: { span: 24 }, xs: { span: 24 } }">
			<a-row :gutter="24">
				<a-col :md="12" :sm="12" :xs="12">
					<a-form-item>
						<div class="hd_left">
							<div class="hd_title">批量录入条码（每行一个）：</div>
							<div class="hd_inp"><a-textarea class="hd_inp_con" placeholder="请输入" v-model:value="val" allow-clear @change="change" /></div>
						</div>
						<div class="fs-14 flex justify-end"><a @click="delrec()">删除重复条码</a></div>
					</a-form-item>
				</a-col>
				<a-col :md="12" :sm="12" :xs="12">
					<a-form-item>
						<div>
							<div class="fs-14 color-999">1、每行一个条码</div>
							<div class="fs-14 margin-top-30"><a @click="clear()">清空条码</a></div>
						</div>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
	</ele-modal>
</template>

<script setup>
import { ref, reactive, unref, onMounted } from 'vue';

const emit = defineEmits(['update:visible', 'luOpt']);
const props = defineProps({
    visible: Boolean
  });

const loading = ref(false);
const val = ref(null);
const bar_code = ref([]);
const bar_code2 = ref([]);
const change = e => {
	bar_code.value = e.target.value.split(/[(\r\n)\r\n]+/);
	bar_code2.value = [];
	bar_code.value.forEach((item, index) => {
		let arr = item.split(',');
		bar_code2.value[index] = {
			bar_code: arr[0],
			num: 1
		};
		if (arr[1]) {
			bar_code2.value[index].num = arr[1];
		}
	});
};
//清空条码
const clear = () => {
	Modal.confirm({
		title: '提示',
		content: '确定清空条码?',
		maskClosable: true,
		onOk: () => {
			val.value = null;
			bar_code.value = [];
			bar_code2.value = [];
		},
		onCancel: () => {}
	});
};
//删除重复条码
const delrec = () => {
	bar_code.value = val.value.split(/[(\r\n)\r\n]+/);
	bar_code.value = norepeat(bar_code.value);
	val.value = bar_code.value.join('\n');
	bar_code2.value = [];
	bar_code.value.forEach((item, index) => {
		let arr = item.split(',');
		bar_code2.value[index] = {
			bar_code: arr[0],
			num: 1
		};
		if (arr[1]) {
			bar_code2.value[index].num = arr[1];
		}
	});
};
const save = () => {
	console.log('bar_code2', bar_code2.value);
	emit('luOpt', bar_code2.value);
};
const norepeat = arr => {
	var newArr = [];
	for (var i = 0; i < arr.length; i++) {
		if (newArr.indexOf(arr[i]) == -1) {
			//解释：如果新数组里没有就放进去，有就放不进了
			newArr.push(arr[i]);
		}
	}
	return newArr;
};
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>

<style></style>
